package com.pw.study.arithmetic

import scala.util.control.Breaks._

/**
 * @Desc: 单链表
 * @author: future
 * @since: 2022/5/25-20:35
 */
object FutureLinkList {
  def main(args: Array[String]): Unit = {
    val node1: HeroNode = new HeroNode(1, "宋江", "及时雨")
    val node2: HeroNode = new HeroNode(5, "武松", "行者")
    val node3: HeroNode = new HeroNode(3, "吴用", "智多星")
    val node4: HeroNode = new HeroNode(2, "燕青", "浪子")

    val list = new SingleLinkedList()
    list.add(node1)
    list.add(node2)
    list.add(node3)
    list.add(node4)
    list.list()
    list.del(1)
    val node22: HeroNode = new HeroNode(2, "武松修改", "修仙")
    list.update(node22)
    list.add2(node2)
    list.list()

  }
}

class HeroNode(id: Int, name: String, alias: String) {
  var no: Int = id
  var nodeNome: String = name
  var nickName: String = alias
  var next: HeroNode = null
}

class SingleLinkedList() {
  def list(): Unit = {
    if (head.next == null) {
      println("链表为空。。。")
      return
    }
    var tmp = head.next
    breakable {
      while (true) {
        if (tmp == null) {
          break()
        }
        println(s"节点信息no${tmp.no},nodeName: ${tmp.nodeNome},ClickName:${tmp.nickName}")
        tmp = tmp.next
      }
    }

  }

  def add2(node: HeroNode): Unit = {
    var tmp = head
    var flag = false
    breakable {
      while (true) {
        if (tmp.next == null) {
          break()
        }
        if (tmp.next.no > node.no) {
          break()
        } else if (tmp.next.no == node.no) {
          flag = true
          break()
        }
        tmp = tmp.next
      }
    }
    if (flag) {
      println(s"插入的编号${node.no} 已经存在")
    } else {
      node.next = tmp.next
      tmp.next = node
    }
  }

  def update(newNode: HeroNode): Unit = {
    if (head.next == null) {
      println("链表为空。。。。")
      return
    }
    var tmp = head.next
    var flag = false
    breakable {
      while (true) {
        if (tmp == null) {
          break()
        }
        if (tmp.no == newNode.no) {
          flag = true
          break()
        }
        tmp = tmp.next
      }
    }
    if (flag) {
      tmp.nodeNome = newNode.nodeNome
      tmp.nickName = newNode.nickName
    } else {
      println(s"没有找到编号: ${newNode.no}")
    }

  }


  def del(n: Int): Unit = {
    var tmp = head
    var flag = false
    breakable {
      while (true) {
        if (tmp.next == null) {
          break()
        }
        if (tmp.next.no == n) {
          flag = true
          break()
        }
        tmp = tmp.next
      }
    }
    if (flag) {
      tmp.next = tmp.next.next

    } else {
      println(s"删除数据no：$n")
    }
  }

  val head = new HeroNode(0, "", "")


  def add(node: HeroNode): Unit = {
    var tmp = head
    breakable {
      while (true) {
        if (tmp.next == null) {
          break()
        }
        tmp = tmp.next
      }
    }
    tmp.next = node
  }

}

object SingleLinkedList {

}
